#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int MAX_TIME=100;
int Frame_time=100;
double prob_error=0;
int error_received_by_node2;
//double prob_error_step;
#define prob_error_step .001
/*------*/
#define TRUE                    1  // Boolean true
#define FALSE                   0  // Boolean false
#define NUM_BITS         10000000  // Number of bits to simulate
#define MAX_BURST               5  // Maximum burst size to count
//#define RAND_MAX                1//
/*------*/
int main()
{
FILE *fp,*fp1,*log;
int time,i,trans_time,index_error_in_rcvd[MAX_TIME],i_prob_error_counter,temp;
double z;
char packet[10],packet_in_error[10];
i_prob_error_counter=0;
temp=1/prob_error_step;
int a_i_retransmission_need_node1[temp];
log=fopen("node1_retrans","w");
for(prob_error;prob_error<=1;prob_error=prob_error+prob_error_step)
{

	fp=fopen("node_1","w+");
	for(time=0;time< (MAX_TIME);time++)
	fprintf(fp,"paket12 %d\n",time);
	fclose(fp);

	//-----packet corruption

	fp1=fopen("node_1","r");
	fp=fopen("node_1_error","w");

	for(i=0;i<(MAX_TIME);i++)
	{
	fscanf(fp1,"%s %d\n",packet,&trans_time);
	printf("Read Packet %s %d\n",packet,trans_time);
	 // Pull a uniform RV between 0 and 1
	    z = (double) rand()/RAND_MAX;
	if (z < prob_error)
	{
	fprintf(fp,"error %d\n",i);
	printf("error in packet %f\n",z);
	}else
	{fprintf(fp,"packet %d\n",i);
	printf("No error %f  %f\n",z,prob_error);
	}

	}
	fclose(fp1);
	fclose(fp);

	//node 2 now reads them
	error_received_by_node2=0;
	fp=fopen("node_1_error","r");
		for(i=0;i<(MAX_TIME);i++)
		{
		fscanf(fp,"%s %d\n",packet,&trans_time);
			if(strcmp(packet,"error")==NULL)
			{
			error_received_by_node2++;
	      		index_error_in_rcvd[i]=0;                
			}else
			{
		     index_error_in_rcvd[i]=1;
			}

		}
	printf("Number of Errors %d\n",error_received_by_node2);
	fclose(fp);

	printf("Packets which needs to be retransmitted again are following");
	for(i=0;i<(MAX_TIME);i++)
	if(index_error_in_rcvd[i]==0)
	printf("%d ",i);
//print: prob of error vs Retransmission needed
a_i_retransmission_need_node1[i_prob_error_counter]=error_received_by_node2;
i_prob_error_counter++;
fprintf(log,"%f %d\n",prob_error,error_received_by_node2);
}





fclose(log);
}
